//
//  NetWorkMngTool.swift
//  约健
//
//  Created by tyh on 2017/9/26.
//  Copyright © 2017年 tyh. All rights reserved.
//

import UIKit

let single = NetWorkMngTool()
var user_id:String = "-1"
var sessionID:String!

class NetWorkMngTool: NSObject {
    
    class func sharedNetworkMngtool() -> NetWorkMngTool{
        return single

}
    //注册
    func RONetwork_Register(_ parameters:NSDictionary, view:UIView, block: @escaping(_ flag:NSString)-> Void){
        
        let afManager = AFHTTPRequestOperationManager()
        
        afManager.post("http://172.24.10.175/workout/api.php/reg", parameters: parameters, success: { (Operation:AFHTTPRequestOperation?,responseObject:Any?) in
            
            
            let model = RegisterModel.RegisterModelWithDict(responseObject as! NSDictionary)
            
            if model.id != nil{
                block("1")
            } else{
                block("0")
            }
            
            
        }, failure: { (Operation:AFHTTPRequestOperation?,error:Error?) in
            
        
            block("0")
        })
    }
    
    //登录
    func RONetwork_Login(_ parameters:NSDictionary, view:UIView, block: @escaping(_ flag:NSString)-> Void){
        
        let afManager = AFHTTPRequestOperationManager()
        
        afManager.post("http://172.24.10.175/workout/api.php/login", parameters: parameters, success: { (Operation:AFHTTPRequestOperation?,responseObject:Any?) in
            
            
            let model = RegisterModel.RegisterModelWithDict(responseObject as! NSDictionary)
            
            if model.id != nil{
                user_id = model.id as String
                sessionID = model.sessionID as String
                print(model.id)
                print(model.sessionID)
                block("1")
            } else{
                block("0")
            }
            
            
        }, failure: { (Operation:AFHTTPRequestOperation?,error:Error?) in
            

            MBProgressHUD.showDelayHUDToView(view, message: "网络问题")
        })
    }

    
    
    //线上课程列表
    
    func Network_GetCourseingListView(_ view:UIView, block: @escaping(_ courseList:NSArray)-> Void){
        let afManager = AFHTTPRequestOperationManager()
        
        afManager.get("http://172.24.10.175/workout/api.php/lists/mod/training", parameters: nil, success: { (Operation:AFHTTPRequestOperation?,responseObject:Any?) in
            
            let courseArray = responseObject as! NSArray
            let courseMutableArray:NSMutableArray = NSMutableArray()
            for courseDict in courseArray{
                let object = courseDict as! NSDictionary
                let course:CourseModel = CourseModel.CourseModelWithDict(object)
                courseMutableArray.add(course)
                
            }
            block(courseMutableArray)
            
            
        }, failure: { (Operation:AFHTTPRequestOperation?,error:Error?) in
            MBProgressHUD.hide(for: view, animated: true)
        })
    }
    
    //线上视频获取
    func Network_GetCourseVideoList(_ parameters:NSDictionary, view:UIView, block: @escaping(_ videoList:NSArray)-> Void){
        
        let afManager = AFHTTPRequestOperationManager()
        
        afManager.post("http://172.24.10.175/workout/api.php/get", parameters: parameters, success: { (Operation:AFHTTPRequestOperation?,responseObject:Any?) in
            
            
            let res = responseObject as! NSDictionary
            let videoArray = res[key:"tvideo"] as! NSArray
            let videoMutableArray:NSMutableArray = NSMutableArray()
            for videoDict in videoArray{
            let object = videoDict as! NSDictionary
            let video:VideoModel = VideoModel.VideoModelWithDict(object)
            videoMutableArray.add(video)
            }
            block(videoMutableArray)
            
        }, failure: { (Operation:AFHTTPRequestOperation?,error:Error?) in
            
            
            MBProgressHUD.showDelayHUDToView(view, message: "网络问题")
        })
    }
    

    
    //线下课程列表
    
    func Network_GetCourseListView(_ view:UIView, block: @escaping(_ courseList:NSArray)-> Void){
        let afManager = AFHTTPRequestOperationManager()
        
        afManager.get("http://172.24.10.175/workout/api.php/lists/mod/train", parameters: nil, success: { (Operation:AFHTTPRequestOperation?,responseObject:Any?) in
            
            let courseArray = responseObject as! NSArray
            let courseMutableArray:NSMutableArray = NSMutableArray()
            for courseDict in courseArray{
                let object = courseDict as! NSDictionary
                let course:CourseingModel = CourseingModel.CourseingModelWithDict(object)
                courseMutableArray.add(course)
                
            }
            
            block(courseMutableArray)
            
            
        }, failure: { (Operation:AFHTTPRequestOperation?,error:Error?) in
            MBProgressHUD.hide(for: view, animated: true)
        })
    }


    //俱乐部列表
    func Network_GetClubsListView(_ view:UIView, block: @escaping(_ courseList:NSArray)-> Void){
        let afManager = AFHTTPRequestOperationManager()
        
        afManager.get("http://172.24.10.175/workout/api.php/lists/mod/club", parameters: nil, success: { (Operation:AFHTTPRequestOperation?,responseObject:Any?) in
            
            let clubArray = responseObject as! NSArray
            let clubMutableArray:NSMutableArray = NSMutableArray()
            for clubDict in clubArray{
                let object = clubDict as! NSDictionary
                let club:ClubModel = ClubModel.ClubModelWithDict(object)
               clubMutableArray.add(club)
                
            }
            block(clubMutableArray)
            
            
        }, failure: { (Operation:AFHTTPRequestOperation?,error:Error?) in
            MBProgressHUD.hide(for: view, animated: true)
        })
    }
    
    //关注俱乐部
    
    
    func Network_FansForClub(_ parameters:NSDictionary, view:UIView, block: @escaping(_ flag:NSString)-> Void){
        
        post(urlStr: "http://172.24.10.175/workout/api.php/create", parameter: parameters){(res) in
            let data = res as! String
            if data > "0"{
                
                block("1")
            }else{
                block("0")
            }
        }
        
        

    }
     //加入俱乐部
    
    func Network_AddClub(_ parameters:NSDictionary, view:UIView, block: @escaping(_ flag:NSString)-> Void){
        
        post(urlStr: "http://172.24.10.175/workout/api.php/create", parameter: parameters) {
            (res) in
            let data = res as! String
            if data > "0"{
                
                block("1")
                
            }
            else{
                block("0")
            }
            
        }
        

    }
    
    
    //is guanzhu club or dashi, 0 is yes, 1 is no
    func Network_IsExits(_ parameters:NSDictionary, view:UIView, block: @escaping(_ flag:NSString)-> Void){
        
        post(urlStr: "http://172.24.10.175/workout/api.php/isExits", parameter: parameters){(res) in
            let data = res as! String
            if data == "0"{
                
                block("1")
            }else{
                block("0")
            }
            
        }

    }
    
    //俱乐部相关视频
    
    func Network_GetClubVideoList(_ parameters:NSDictionary, view:UIView, block: @escaping(_ videoList:NSArray)-> Void){
        
        let afManager = AFHTTPRequestOperationManager()
        
        afManager.post("http://172.24.10.175/workout/api.php/get", parameters: parameters, success: { (Operation:AFHTTPRequestOperation?,responseObject:Any?) in
            
            
            let res = responseObject as! NSDictionary
            let videoArray = res[key:"Clubvideo"] as! NSArray
            let videoMutableArray:NSMutableArray = NSMutableArray()
            for videoDict in videoArray{
                let object = videoDict as! NSDictionary
                let clubvideo:ClubVideoModel = ClubVideoModel.VideoModelWithDict(object)
                videoMutableArray.add(clubvideo)
            }
            block(videoMutableArray)
            
        }, failure: { (Operation:AFHTTPRequestOperation?,error:Error?) in
            
            
            MBProgressHUD.showDelayHUDToView(view, message: "网络问题")
        })
    }

    //俱乐部相关教练
    func Network_GetClubTeacherList(_ parameters:NSDictionary, view:UIView, block: @escaping(_ videoList:NSArray)-> Void){
        
        let afManager = AFHTTPRequestOperationManager()
        
        afManager.post("http://172.24.10.175/workout/api.php/get", parameters: parameters, success: { (Operation:AFHTTPRequestOperation?,responseObject:Any?) in
            
            
            let res = responseObject as! NSDictionary
            let teacherArray = res[key:"Clubteacher"] as! NSArray
            let teacherMutableArray:NSMutableArray = NSMutableArray()
            for teacherDict in teacherArray{
                let object = teacherDict as! NSDictionary
                let clubteacher:ClubClubteacherModel = ClubClubteacherModel.TeacherModelWithDict(object)
                teacherMutableArray.add(clubteacher)
            }
            block(teacherMutableArray)
            
        }, failure: { (Operation:AFHTTPRequestOperation?,error:Error?) in
            
            
            MBProgressHUD.showDelayHUDToView(view, message: "网络问题")
        })
    }
    
    //俱乐部相关课程
    func Network_GetClubCourseList(_ parameters:NSDictionary, view:UIView, block: @escaping(_ videoList:NSArray)-> Void){
        
        let afManager = AFHTTPRequestOperationManager()
        
        afManager.post("http://172.24.10.175/workout/api.php/get", parameters: parameters, success: { (Operation:AFHTTPRequestOperation?,responseObject:Any?) in
            
            
            let res = responseObject as! NSDictionary
            let courseArray = res[key:"Clubcourse"] as! NSArray
            let courseMutableArray:NSMutableArray = NSMutableArray()
            for courseDict in courseArray{
                let object = courseDict as! NSDictionary
                let clubcourse:ClubcourseModel = ClubcourseModel.CourseModelWithDict(object)
                courseMutableArray.add(clubcourse)
            }
            block(courseMutableArray)
            
        }, failure: { (Operation:AFHTTPRequestOperation?,error:Error?) in
            
            
            MBProgressHUD.showDelayHUDToView(view, message: "网络问题")
        })
    }
    //大师列表
    func Network_GetDaShiList(_ parameters:NSDictionary, view:UIView, block: @escaping(_ dashiList:NSArray)-> Void){
        
        let afManager = AFHTTPRequestOperationManager()
        
        afManager.post("http://172.24.10.175/workout/api.php/lists", parameters: parameters, success: { (Operation:AFHTTPRequestOperation?,responseObject:Any?) in
            
            
            let res = responseObject as! NSArray
            let dashiMutableArray:NSMutableArray = NSMutableArray()
            for dashiDict in res{
                let object = dashiDict as! NSDictionary
                let clubcourse:DaShiModel = DaShiModel.DaShiModelWithDict(object)
                dashiMutableArray.add(clubcourse)
            }
            block(dashiMutableArray)
            
        }, failure: { (Operation:AFHTTPRequestOperation?,error:Error?) in
            
            
            MBProgressHUD.showDelayHUDToView(view, message: "网络问题")
        })
    }
    
    //活动列表
    func Network_GetHuoDongList(_ parameters:NSDictionary, view:UIView, block: @escaping(_ dashiList:NSArray)-> Void){
        
        let afManager = AFHTTPRequestOperationManager()
        
        afManager.post("http://172.24.10.175/workout/api.php/lists", parameters: parameters, success: { (Operation:AFHTTPRequestOperation?,responseObject:Any?) in
            
            
            let res = responseObject as! NSArray
            let dashiMutableArray:NSMutableArray = NSMutableArray()
            for dashiDict in res{
                let object = dashiDict as! NSDictionary
                let clubcourse:HuoDongModel = HuoDongModel.HuoDongModelWithDict(object)
                dashiMutableArray.add(clubcourse)
            }
            block(dashiMutableArray)
            
        }, failure: { (Operation:AFHTTPRequestOperation?,error:Error?) in
            
            
            MBProgressHUD.showDelayHUDToView(view, message: "网络问题")
        })
    }

    
    //晒图列表
    func Network_GetShaiTuList(_ parameters:NSDictionary, view:UIView, block: @escaping(_ dashiList:NSArray)-> Void){
        
        let afManager = AFHTTPRequestOperationManager()
        
        afManager.post("http://172.24.10.175/workout/api.php/lists", parameters: parameters, success: { (Operation:AFHTTPRequestOperation?,responseObject:Any?) in
            
            
            let res = responseObject as! NSArray
            let dashiMutableArray:NSMutableArray = NSMutableArray()
            for dashiDict in res{
                let object = dashiDict as! NSDictionary
                let clubcourse:ShaiTuModel = ShaiTuModel.ShaiTuModelWithDict(object)
                dashiMutableArray.add(clubcourse)
            }
            block(dashiMutableArray)
            
        }, failure: { (Operation:AFHTTPRequestOperation?,error:Error?) in
            
            
            MBProgressHUD.showDelayHUDToView(view, message: "网络问题")
        })
    }

    
    
    
    
    //Post方法 接受数字返回值
    private func post(urlStr:String,parameter:NSDictionary,block:((_ data:Any?)->Void)?){
        let paraArray = NSMutableArray()
        for (key,value) in parameter{
            let str = "\(key)=\(value)"
            paraArray.add(str)
        }
        let body = paraArray.componentsJoined(by: "&")
        var request = URLRequest(url: URL(string: urlStr)!)
        request.httpMethod = "post"
        request.httpBody = body.data(using: String.Encoding.utf8)
        
        let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
            if data != nil{
                if let json = try? JSONSerialization.jsonObject(with: data!, options: .mutableContainers){
                    block!(json)
                }
                else{
                    if let str = String(data: data!, encoding: String.Encoding(rawValue: CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(CFStringEncodings.GB_18030_2000.rawValue)))){
                        print(str)
                        block!(str)
                    }else if let str = String(data: data!, encoding: .utf8){
                        print(str)
                        block!(str)
                    }
                }
            }else{
                print("返回为空")
                print(error.debugDescription)
            }
        }
        task.resume()
    }

}
